package j_util.graphics.auxiliary;

import j_util.Utiles;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
public class GraphicsUtil {
    private double scale;
    private ScaleMeter scaleMeter;

    private static double[] x;
    private static double[] y;
    private static double lastFrequency = 1;


    public static String getConfigFileData(String directoryPath) {
        String path = Utiles.createDirectory("" + directoryPath);
        long name = 0;
        try {
            RandomAccessFile randomAccessFile = new RandomAccessFile(path + "/graphConfig.txt", "rw");
            name = randomAccessFile.readLong();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("" + directoryPath);
        path = Utiles.createDirectory(directoryPath + "/graphics" + name);
        System.out.println("" + path);
        return Utiles.createDirectory(directoryPath + "/graphics" + name);
    }

    public static void updateConfigFile(String directoryPath) {
        String path = Utiles.createDirectory("" + directoryPath);
        RandomAccessFile randomAccessFile = null;
        try {
            randomAccessFile = new RandomAccessFile(path + "/graphConfig.txt", "rw");
            long name = 0;
            try {
                name = randomAccessFile.readLong();
            } catch (IOException e) {
                try {
                    randomAccessFile.seek(0);
                    randomAccessFile.writeLong(name);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                e.printStackTrace();
            }
            System.out.println("" + new File(path + "/graphics" + name).isDirectory());
            if (new File(path + "/graphics" + name).isDirectory()) {
                try {
                    randomAccessFile.seek(0);
                    randomAccessFile.writeLong(name + 1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    randomAccessFile.seek(0);
                    randomAccessFile.writeLong(name);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static double[] getXValues() {
        if (x == null) {
            x = new double[700];
            int value = -350;
            for (int j = 0; j < 700; j++) {
                x[j] = value;
                value++;
            }
        }
        return x;
    }

    public static double[] getYValues(double[] number, double scale) {
        if (y == null) {
            y = new double[700];
        }
        int j;
        int length;
        if (number == null) {
            length = 0;
        } else {
            length = number.length;
        }
        for (j = 0; j < y.length - length; j++) {
            y[0] = y[length + j];
        }
        for (j = 0; j < length; j++) {
            y[y.length - length + j] = number[j] * 10 / scale;
        }
        return y;
    }

    public static double[] getYValues(double frequency, double scale, boolean start) {
        double halfPeriod;
        if (frequency != 0) {
            lastFrequency = frequency;
        }
        if (lastFrequency < scale) {//check in order to get better results
            halfPeriod = 10 / (scale * lastFrequency);
        } else {
            halfPeriod = 10 / (lastFrequency * scale);
        }
        double[] data = new double[(int) (2 * halfPeriod)];
        if (start) {
            if (frequency != 0) {
                for (int j = 0; j < (int) halfPeriod; j++) {
                    data[j] = 1;
                }
            } else {
                for (int j = 0; j < (int) halfPeriod; j++) {
                    data[j] = 0;
                }
            }
        } else {
            if (frequency != 0) {
                for (int j = (int) halfPeriod; j < data.length; j++) {
                    data[j] = 1;
                }
            } else {
                for (int j = (int) halfPeriod; j < data.length; j++) {
                    data[j] = 0;
                }
            }
        }
        return getYValues(data, scale);
    }

    public static double[] updateYScale(double scale) {
        for (int j = 0; j < 700; j++) {
            y[j] = y[j] * 10 / scale;
        }
        return y;
    }

    public static double[] updateXScale(double scale) {
        int equivalence = (int) (10 / scale);
        double[] newX = new double[700];
        int interval = 700 / equivalence;
        for (int j = 0; j < interval; j++) {
            for (int l = 0; l < equivalence; l++) {
                newX[700 - j * l - 1] = x[700 - j - 1];
            }
        }
        return newX;
    }

    public static void resetGraphicMemory() {
        x = new double[700];
        y = new double[700];
    }
}
